<template>
  <div class="login-container">
    <el-form
      ref="form"
      class="login-form"
      :model="form"
      :rules="regRules"
      autocomplete="on"
      label-position="left"
      style="padding-top:100px"
    >
      <div class="title-container">
        <h3 class="title">
          <img src="../../assets/login/logo.png" />
        </h3>
        <lang-select class="set-language" />
      </div>
      <el-form-item prop="name">
        <span class="svg-container">
          <svg-icon icon-class="drag" />
        </span>
        <el-input
          v-model="form.name"
          :placeholder="$t('register.name')"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="phone">
        <span class="svg-container">
          <svg-icon icon-class="drag" />
        </span>
        <el-input
          v-model="form.phone"
          :placeholder="$t('register.phone')"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="verifyCode">
        <el-row>
          <el-col :span="20"
            ><div class="grid-content bg-purple">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                v-model="form.verifyCode"
                :placeholder="$t('register.phonecode')"
                type="text"
              /></div
          ></el-col>
          <el-col :span="4"
            ><div class="grid-content bg-purple-light">
              <span v-show="show" @click="getCode" style="padding-right:5px"
                >获取验证码</span
              >
              <span v-show="!show" class="count">{{ count }} s</span>
            </div></el-col
          >
        </el-row>
      </el-form-item>
      <el-form-item prop="upass">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          name="password"
          v-model="form.upass"
          :type="passwordType"
          :placeholder="$t('register.pwd')"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <el-form-item prop="confpwd" style="margin-bottom:10px">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          name="password"
          v-model="form.confpwd"
          :type="passwordType"
          :placeholder="$t('register.quepwd')"
          tabindex="2"
          autocomplete="on"
        /><span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <el-radio-group v-model="form.partyType">
        <el-radio :label="0">司机</el-radio>
        <el-radio :label="1">车队物流公司</el-radio>
        <el-radio :label="2">发货人</el-radio>
        <el-radio :label="3">发货企业</el-radio>
      </el-radio-group>

      <el-button
        type="primary"
        style="width:100%;margin-bottom:30px;margin-bottom:10px"
        @click="registerbut('form')"
        >{{ $t("register.register") }}</el-button
      >
      <el-row>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-checkbox v-model="checked">我已阅读并同意</el-checkbox>
          </div>
        </el-col>
        <el-col :span="16">
          <div class="grid-content bg-purple zhengce">
            <a>《用户服务协议》</a><a :href="url" target="_blank">《隐私政策》</a><a>《用户授权协议》</a>
          </div></el-col
        >
      </el-row>
    </el-form>
    <div class="footer">
      <img src="../../assets/login/footerimg.png" />
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import LangSelect from "@/components/LangSelect";
import { validatePhone } from "@/utils/validate.js";
import { Verifyphone, register, Getverificationcode } from "@/api/user";

export default {
  components: { LangSelect },
  data() {
    return {
      url: this.BASE_API_URL + 'agreement/privacyAgreement.html',
      passwordType: "password",
      show: true,
      count: "",
      checked: false,
      timer: null,
      type: [],
      passwordType: "password",
      form: {
        phone: "",
        name: "",
        verifyCode: "",
        upass: "",
        branchCode: "",
        partyType: 0
      },
      regRules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        verifyCode: [
          { required: true, message: "请输入验证码", trigger: "blur" }
        ],
        upass: [{ required: true, message: "请输入密码", trigger: "blur" }],
        confpwd: [
          { required: true, message: "请输入确认密码", trigger: "blur" }
        ],
        type: [{ required: true, message: "请勾选协议", trigger: "blur" }]
      }
    };
  },
  created() {},
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
    },
    getCode() {
      const cationCode = {
        partyGroupCode: "",
        phone: this.form.phone
      };
      if (!validatePhone(this.form.phone)) {
        this.$message.error("请输入正确的手机号");
        return false;
      } else {
        Getverificationcode(cationCode).then(response => {
          if (response.status == 0) {
            const TIME_COUNT = 60;
            if (!this.timer) {
              this.count = TIME_COUNT;
              this.show = false;
              this.timer = setInterval(() => {
                if (this.count > 0 && this.count <= TIME_COUNT) {
                  this.count--;
                } else {
                  this.show = true;
                  clearInterval(this.timer);
                  this.timer = null;
                }
              }, 1000);
            }
          }
        });
      }
    },
    //注册
    registerbut(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          if (this.form.upass != this.form.confpwd) {
            this.$message.error("两次密码输入不一致");
            return false;
          } else {
            if (this.checked == true) {
              Verifyphone(this.form.phone).then(response => {
                if (response.status == 0) {
                  register(this.form).then(response => {
                    if (response.status == 0) {
                      this.$router.push({
                        path: "/login"
                      });
                    } else {
                      this.$message({ message: response.msg, type: "success" });
                    }
                  });
                }
              });
            } else {
              this.$message.error("请查看相关协议，并勾选");
            }
          }
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  //background-color: $bg;
  //overflow: hidden;
  background: url("../../assets/login/loginimg.png");
  background-size: 100% 100%;
  height: 100%;
  position: fixed;
  width: 100%;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      img {
        width: 80%;
      }
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
.footer {
  position: fixed;
  bottom: 50px;
  left: 50%;
  margin-left: -100px;
  img {
    width: 70%;
  }
}
.bg-purple-light {
  span {
    color: #fff;
    line-height: 50px;
    font-size: 13px;
    text-align: right;
    display: block;
  }
}
.el-radio-group {
  margin: 10px 0;
}
.el-radio__inner {
  background-color: #f00;
}
.zhengce {
  font-size: 13px;
  color: #1890ff;
}
</style>
